package com.wf.course.web.controller.comm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.utils.page.PageBean;
import com.wf.course.model.comm.MessageInfo;
import com.wf.course.model.member.Member;
import com.wf.course.service.IMemberService;
import com.wf.course.service.IMessageInfoService;
import com.wf.course.web.Interceptor.LogInfo;

/**
 *短信通知
 */

@RequestMapping("message")
@Controller
public class MessageInfoController {

	@Autowired
	private IMessageInfoService msgService;
	@Autowired
	private IMemberService memberService;
	
	
	 /***
	  * 短信通知
	  * @param pageNo
	  * @param pageSize
	  * @param sendStatus
	  * @param model
	  * @return
	  */
	@RequestMapping("messageList")
	public String imgList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
						  @RequestParam(value = "pageSize", required = false) Integer pageSize,
						  @RequestParam(value = "telephone", required = false) String telephone,
						  Model model){
		Map<String,Object> map=new HashMap<String,Object>(); 
		map.put("telephone",telephone);
		
		PageBean<MessageInfo> pageBean = new PageBean<>(pageNo, pageSize,null,map);
		msgService.getMessageInfoList(pageBean);
		
		pageBean.setUrl("/message/messageList");
		model.addAttribute("page", pageBean);
		return "comm/messageList";
	}
	/**
	 * 短信通知：新增修改页面信息加载
	 * @param id
	 * @return
	 */
	@LogInfo("发送短信，页面数据初始化")
	@RequestMapping("downLoadMessage/{id}")
	public String downLoadMessageInfo(@PathVariable("id") Integer id,Model model){
		 if(id!=null && id!=0){
			 MessageInfo MessageInfo = this.msgService.downLoadMessageInfo(id);
				model.addAttribute("MessageInfo", MessageInfo);
				model.addAttribute("type", "upd");
		 }else{
			 model.addAttribute("type", "add");
		 }
		 
		return "comm/messageEdit";	
		
	}
	
	
	/**
	 * 群发短信
	 * 1.根据发送对象，查询用户列表
	 * 2.循环发送短信（发送成功，继续下一个用户，发送失败，保存失败记录）
	 * @param MessageInfo
	 * @param request
	 * @return
	 */
	@LogInfo("群发短信")	 
	@RequestMapping(value = {"sendMessage" } )
	public String MessageInfoEdit(@ModelAttribute MessageInfo messageInfo, HttpServletRequest request){
		
		String goodsName = messageInfo.getGoodsName();
		List<Member> members = new ArrayList<Member>();
		if(messageInfo.getGoalMembers()==1){
			//查询所有在平台上注册的用户
			members = memberService.queryMembers();
		}else if(messageInfo.getGoalMembers()==2){
			//查询所有在平台消费的用户
			members = memberService.queryCustoms();
		}
		//循环，发送信息
		if(members!=null && members.size()>0){
		try {
			for(int i=0;i<members.size();i++){
				String userName = members.get(i).getUserName();
				userName = userName == null ? "用户" : userName;
				String telephone = members.get(i).getTelephone();
				HashMap<String, Object> result = msgService.sendMsg(telephone, userName, goodsName);
				if(result!= null && "000000".equals(result.get("statusCode").toString())){
					//如果发送成功，继续下一个用户
					continue;
				}else{
					//如果发送失败，保存失败记录
					messageInfo.setUserName(userName);
					messageInfo.setTelephone(telephone);
					messageInfo.setSendStatus(result.get("statusCode").toString());
					messageInfo.setDetail(result.get("statusMsg").toString());
					messageInfo.setGoodsName(goodsName);
					this.msgService.saveMessageInfo(messageInfo);
				}
			}
		}catch(Exception e1){
			e1.printStackTrace();
		}
			
		}
		return "redirect:/message/messageList";
		
	}
	
	/**
	 * 单独发送
	 * @param id
	 * @return
	 */
	@LogInfo("单独发送")
	@RequestMapping(value="sendMessageByTel/{id}" , produces = "text/html;charset=UTF-8")
	public String sendMessageByTel(@PathVariable("id") Integer id,	HttpServletRequest request){
		 
		try {
			MessageInfo messageInfo = this.msgService.downLoadMessageInfo(id);
		 
		 	String userName = messageInfo.getUserName() == null ? "用户" : messageInfo.getUserName();
		 
 		 	HashMap<String, Object> result = msgService.sendMsg(messageInfo.getTelephone(), 
 				userName, messageInfo.getGoodsName());
			if("000000".equals(result.get("statusCode").toString())){
				//如果发送成功，删除本条记录
				msgService.deleteMessageInfo(messageInfo.getId());
				 
			}else{
				//如果发送失败，保存失败记录
				messageInfo.setUserName(messageInfo.getUserName());
				messageInfo.setTelephone(messageInfo.getTelephone());
				messageInfo.setSendStatus(result.get("statusCode").toString());
				messageInfo.setDetail(result.get("statusMsg").toString());
				messageInfo.setGoodsName(messageInfo.getGoodsName());
				
				this.msgService.saveMessageInfo(messageInfo);
				
			}
		}catch(Exception e1){
			e1.printStackTrace();
		}
			
		return "redirect:/message/messageList";
	}
	
	
}
